package com.jevoly.config;

import com.jevoly.api.ServerController;
import com.jevoly.listener.DirectoryListener;
import com.jevoly.listener.DirectoryMonitor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class InitConfig implements ApplicationRunner{

    private static Log log = LogFactory.getLog(InitConfig.class);

    @Autowired
    ServerController serverController;

    // 注入本地图片文件夹路径
    @Value("${params.localAlbum}")
    String localAlbum;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("系统初始化...");
        serverController.copyDirectoryToServer(localAlbum);
        log.info("文件已经准备就绪...");
        log.info("开始监听Album文件夹...");
        startMoniter();
    }

    private void startMoniter() {
        DirectoryMonitor directoryMonitor = null;
        try {
            directoryMonitor = new DirectoryMonitor(1000);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("创建目录监听时发生错误...");
        }
        directoryMonitor.monitor(localAlbum, new DirectoryListener());
        try {
            directoryMonitor.start();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("启动目录监听时发生错误...");
        }
    }
}
 